#!/usr/bin/env python
#coding=utf-8
import os
import sqlite3
import wx.html
import wx
import wx.lib.flatnotebook as fnb
import wx.lib.dialogs
import wx.lib.buttonpanel as bp
import TreeCtrl
import Splitter
import Globals
myEVT_RIGHTTREE_CREATED = wx.NewEventType() #2 创建一个事件类型  
EVT_RIGHTTREE_CREATED = wx.PyEventBinder(myEVT_RIGHTTREE_CREATED, 1)  #3 创建一个绑定器对象
myEVT_LEFTTREE_CREATED = wx.NewEventType() #2 创建一个事件类型  
EVT_LEFTTREE_CREATED = wx.PyEventBinder(myEVT_LEFTTREE_CREATED, 1)  #3 创建一个绑定器对象

class TreeCreatedEvent(wx.PyCommandEvent):   #1 定义事件          
    def __init__(self, evtType, id):                
        wx.PyCommandEvent.__init__(self, evtType, id)
        self.eventArgs = None
       
    def GetEventArgs(self):
        return self.eventArgs

    def SetEventArgs(self, args):
        self.eventArgs = args


class View(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,u"中国开发网－离线浏览器",pos=wx.Point(0, 0),size=(800,610))     
        self.toolBtnNames = {"bp_btn1.png":u"隐藏导航","bp_btn2.png":u"显示导航","bp_btn3.png":u"进入版面","bp_btn4.png":u"从服务器刷新"}
        #转义字符列表
        self._CurrBoradName = ""
        #由该变量决定，右侧的sizer会在第二次的时候进行layout
        self._MyFlag = False
        def CreateStatusBar():
            self.statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
            self.statusbar.SetStatusWidths([-2, -1])
            statusbar_fields = [(u"cndev离线浏览器"),(u"作者：龙之吻 (tingsking@gmail.com)")]
            for i in range(len(statusbar_fields)):
                self.statusbar.SetStatusText(statusbar_fields[i], i)
        CreateStatusBar()      
        self.mainPanel = wx.Panel(self, -1)   
        self.splitter = Splitter.Splitter(self.mainPanel, -1)    
        self.splitter.SetMinimumPaneSize(0)
        self.topPanel = wx.Panel(self.mainPanel, -1) 
        self.titleBar = bp.ButtonPanel(self.topPanel, id = -1,style=bp.BP_DEFAULT_STYLE, alignment=1, name = 'tedst11')
        self.CreatePageButtons()
        self.CreateToolBarBtns()
        vSizer = wx.BoxSizer(wx.VERTICAL) 
        self.mainPanel.SetSizer(vSizer) 
        vSizer.Add(self.topPanel, 0, wx.EXPAND)
        vSizer.Add((1, 1))
        vSizer.Add(self.splitter, 1, wx.EXPAND|wx.ALL, 1)
        vSizer.Layout()
        self.mainPanel.Layout()
        self.titleBar.DoLayout()
        #生成分割条       
        sty = wx.BORDER_SUNKEN 
        #self.p1 = wx.Window(self.splitter, style=sty)
        #self.p1.SetBackgroundColour("pink")
        self.p2 = wx.Window(self.splitter, style=sty)
        #self.p2.SetBackgroundColour("sky blue")
        self.SetIcon(wx.Icon(os.path.join(Globals.GetCurrDir(), 'img', 'plone.ico'), wx.BITMAP_TYPE_ICO))    
        self.Center()
        self.Maximize()   
    #构建左边的版面导航树
    def CreateLeftTree(self):
        self.leftTree = TreeCtrl.CustomTreeCtrl(id=wx.ID_ANY, parent=self.splitter,size=wx.Size(2510, 2210),
                                                pos=wx.DefaultPosition,
                                                style=wx.BORDER_SUNKEN | wx.TR_HAS_BUTTONS | wx.TR_HAS_VARIABLE_ROW_HEIGHT)
        self.splitter.SplitVertically(self.leftTree, self.p2, 170)
        self.leftTree.SetIndent(3)
        img = wx.Bitmap(os.path.join(Globals.GetCurrDir(), 'img', "bkground.gif"), wx.BITMAP_TYPE_GIF)
        self.leftTree.SetBackgroundImage(img)
        self.CreateDataImgLst()        
        #初始化左边的导航树
        plone = self.leftTree.AddRoot(u"版面导航")
        self.imageList = wx.ImageList(16,16)
        self.leftTree.SetItemImage(plone,0 , wx.TreeItemIcon_Normal)
        self.leftTree.SetItemImage(plone,0 , wx.TreeItemIcon_Selected)
        for i in range(2,5):
            img = wx.Bitmap(os.path.join(Globals.GetCurrDir(), 'img', str(i)+'.bmp'), wx.BITMAP_TYPE_BMP)
            self.imageList.Add(img)
        self.leftTree.SetImageList(self.imageList)
        evt = TreeCreatedEvent(myEVT_LEFTTREE_CREATED, self.leftTree.GetId()) #5 创建自定义事件对象  
        evt.SetEventArgs(self.leftTree)   # 6添加数据到事件
        self.GetEventHandler().ProcessEvent(evt)  #7 处理事件
        
    def CreatePageButtons(self):
        #创建几个BoxSizer用于将分页按钮固定
        TopRightPanel = wx.Panel(self.topPanel, -1) 
        #self.TopRightPanel.SetBackgroundColour("pink")
        tSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.topPanel.SetSizer(tSizer)
        tSizer.Add(self.titleBar,0,wx.EXPAND)
        tSizer.Add(TopRightPanel,1, wx.EXPAND|wx.ALL, 1)
        tSizer.Layout()
        self.topPanel.Layout()        
        TopTopPnl = wx.Panel(TopRightPanel,size = wx.Size(1,25), id=-1) 
        #self.TopTopPnl.SetBackgroundColour("red")
        self.pageButtonPnl = wx.Panel(TopRightPanel, -1) 
        #self.pageButtonPnl.SetBackgroundColour("pink")
        ttSizer = wx.BoxSizer(wx.VERTICAL) 
        TopRightPanel.SetSizer(ttSizer)
        ttSizer.Add(TopTopPnl,0,wx.EXPAND)
        ttSizer.Add(self.pageButtonPnl,1, wx.EXPAND|wx.ALL, 1)
        ttSizer.Layout()
        TopRightPanel.Layout()        
        pSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.pageButtonPnl.SetSizer(pSizer)
        tmpPnl = wx.Panel(self.pageButtonPnl,size = wx.Size(50,1), id=-1) 
        pSizer.Add(tmpPnl)
        #创建四个导航按钮
        for label in [u"<<第一页",u"<上一页",u">下一页"]:
            button = wx.Button(self.pageButtonPnl,-1, label, style=wx.NO_BORDER)
            pSizer.Add(button)
        pSizer.Layout()
        self.pageButtonPnl.Layout()    
            
    def CreateDataImgLst(self):
        #初始化论坛表情的imagelist
        self.msgImgList = wx.ImageList(15,15)
        self.imgDataDict = {}
        index = 0
        nums = [[7000,7002,1],[8010,8205,10],[9001,9059,1]]
        for num in nums:
            for i in range(num[0],num[1],num[2]):
                img = wx.Bitmap(os.path.join(Globals.GetCurrDir(), 'img', 'face'+str(i)+'.gif'), wx.BITMAP_TYPE_GIF)
                self.imgDataDict['face'+str(i)+'.gif'] = str(index)
                self.msgImgList.Add(img) 
                index = index+1        
    #构建右边的详细树继承自wx.lib.customtreectrl
    def CreateRightTree(self,kind):
        if self._MyFlag:
            self.rightSplitter.Destroy()
        #右边的分割条
        #设置sizer的layout
        rSizer = wx.BoxSizer(wx.VERTICAL)   
        self.p2.SetSizer(rSizer) 
        self.rightSplitter = Splitter.Splitter(self.p2, -1)
        rSizer.Add(self.rightSplitter, 1, wx.EXPAND|wx.ALL, 1)
        rSizer.Layout()
        self.p2.Layout()
        self.rightTree = TreeCtrl.CustomView(self.rightSplitter,kind =kind)
        #下面开始创建树的图像
        img = wx.Bitmap(os.path.join(Globals.GetCurrDir(), 'img', "bkground.gif"), wx.BITMAP_TYPE_GIF)
        self.rightTree.SetBackgroundImage(img)
        self.rightTree.SetImageList(self.msgImgList)    
        self.notebook = fnb.FlatNotebook(parent=self.rightSplitter,id = -1,style=fnb.FNB_NODRAG | #fnb.FNB_X_ON_TAB |
        fnb.FNB_VC8 | fnb.FNB_NO_X_BUTTON | fnb.FNB_NO_NAV_BUTTONS)
        self.notebook.SetActiveTabColour("green")
        self.notebook.AddPage(self.CreatePage(u"帖子内容"), u"帖子内容", True)
        evt = TreeCreatedEvent(myEVT_RIGHTTREE_CREATED, self.rightTree.GetId()) #5 创建自定义事件对象  
        evt.SetEventArgs(self.rightTree.GetTree())   # 6添加数据到事件
        self.GetEventHandler().ProcessEvent(evt)  #7 处理事件
        self.rightSplitter.SetMinimumPaneSize(20)
        self.rightSplitter.SplitHorizontally(self.rightTree.GetTree(), self.notebook, -150)
        self._MyFlag = True
    def CreatePage(self, caption):
        p = wx.Panel(self.notebook)
        rSizer = wx.BoxSizer(wx.VERTICAL)   
        p.SetSizer(rSizer) 
        self.detailPage = wx.html.HtmlWindow(parent=p, style=wx.BORDER_SUNKEN | wx.html.HW_SCROLLBAR_AUTO)
        rSizer.Add(self.detailPage, 1, wx.EXPAND|wx.ALL, 1)
        rSizer.Layout()
        p.Layout() 
        return p

    def CreateToolBarBtns(self):
        #隐藏导航按钮
        strName = "bp_btn1.png"
        png = wx.Image(os.path.join(Globals.GetCurrDir(), 'img', strName),wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.hidebtn = bp.ButtonInfo(self.titleBar, id = wx.NewId(),bmp =png,text = self.toolBtnNames[strName], kind=wx.ITEM_NORMAL,shortHelp=self.toolBtnNames[strName], longHelp=self.toolBtnNames[strName])
        self.titleBar.AddButton(self.hidebtn)
        self.titleBar.AddSeparator() 
        #刷新按钮
        strName = "bp_btn4.png"
        png = wx.Image(os.path.join(Globals.GetCurrDir(), 'img', strName),wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.refreshBtn = bp.ButtonInfo(self.titleBar, id = wx.NewId(),bmp =png,text = self.toolBtnNames[strName], kind=wx.ITEM_NORMAL,shortHelp=self.toolBtnNames[strName], longHelp=self.toolBtnNames[strName])
        self.titleBar.AddButton(self.refreshBtn)
        self.titleBar.AddSeparator() 
        #进入版面按钮
        strName = "bp_btn3.png"
        png = wx.Image(os.path.join(Globals.GetCurrDir(), 'img', strName),wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        self.enterbtn = bp.ButtonInfo(self.titleBar, id = wx.NewId(),bmp =png,text = self.toolBtnNames[strName], kind=wx.ITEM_NORMAL,shortHelp=self.toolBtnNames[strName], longHelp=self.toolBtnNames[strName])
        self.titleBar.AddButton(self.enterbtn)
        self.enterChoice = wx.Choice(self.titleBar, -1)
        self.titleBar.AddControl(self.enterChoice)
        self.titleBar.AddSeparator()
        #查询框
        self.search = wx.SearchCtrl(self.titleBar,id = -1,  style=wx.TE_PROCESS_ENTER)
        self.search.ShowCancelButton(True)
        self.titleBar.AddControl(wx.StaticText(self.titleBar,id = -1,label = u"查询："))
        self.titleBar.AddControl(self.search)
        self.titleBar.AddSeparator()
        #选择显示模式
        self.rbDisplayModel = wx.RadioBox(self.titleBar, -1, u"显示模式", wx.DefaultPosition, wx.DefaultSize,[u"列表显示",u"树形显示"])
        #self.rbDisplayModel.SetSelection(1)
        self.titleBar.AddControl(self.rbDisplayModel)
        self.titleBar.AddSeparator()
        
